#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
#include <algorithm>
using namespace std;
  struct Interval {
 	int start;
 	int end;
 	Interval(int s, int e) : start(start), end(e) {}
    Interval(){}
  };
 
class Comp
{
public:
    bool operator()(Interval a, Interval b)
    {
        return a.start < b.start;
    }
};
class Solution {
public:
    vector<Interval> merge(vector<Interval>& intervals)
    {
        sort(intervals.begin(), intervals.end(), Comp());
        vector<Interval> ret;
        if (intervals.size() == 0) return ret;
        ret.push_back(intervals[0]);
        for (int i = 1;i < intervals.size();++i)
        {
            if (intervals[i].start >= ret.rbegin()->start
                && intervals[i].start <= ret.rbegin()->end)
            {
                Interval tmp;
                tmp.start = intervals[i].start < ret.rbegin()->start ?
                    intervals[i].start : ret.rbegin()->start;
                tmp.end = intervals[i].end > ret.rbegin()->end ?
                    intervals[i].end : ret.rbegin()->end;
                ret.pop_back();
                ret.push_back(tmp);
            }
            else
            {
                ret.push_back(intervals[i]);
            }
        }
        return ret;
    }
};